var weeklyReportTable = function () {
    return {
        init: function () {
            weeklyReportTable._query();
            weeklyReportTable._initDate();
            weeklyReportTable._initTable();
            weeklyReportTable._export();
            weeklyReportTable._showDetail();
        },
        _initTable: function () {
            var tableId = "weeklyReport_table";
            util.initCheckBox(tableId);

            function queryData(sSource, aoData, fnCallback) {
                aoData.push({
                    "name": "startDate",
                    "value": $("#gatherTime_start").val().trim() + " 00:00:00"
                });
                aoData.push({
                    "name": "endDate",
                    "value": $("#gatherTime_end").val().trim() + " 23:59:59"
                });
                $.ajax({
                    "type": "post",
                    "url": sSource,
                    "dataType": "json",
                    "data": {
                        "aoData": JSON.stringify(aoData)
                    },
                    "success": function (resp) {
                        fnCallback(resp);
                    }
                });
            }

            $('#weeklyReport_table').DataTable({
                "processing": true,
                "serverSide": true,
                "sAjaxSource": "main/report/weekly/getWeeklyReport.mvc",
                "fnServerData": queryData,
                "pageLength": 10,
                "sDom": "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
                "oLanguage": util.getDToLanguage(),
                "columns": [
                    {
                        "data": "index",//序号
                        "bSortable": false,
                        "render": function (data, type, row, meta) {
                            var startIndex = meta.settings._iDisplayStart;
                            return startIndex + meta.row + 1;
                        }
                    },
                    {
                        "data": "packCode",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "140px"
                    }, {
                        "data": "colClientId",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "85px"
                    }, {
                        "data": "packModel",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "productDateStr",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "85px"

                    }, {
                        "data": "startDateStr",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "85px"
                    }, {
                        "data": "endDateStr",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "85px"
                    }, {
                        "data": "alertCount",
                        "bSortable": false,
                        "defaultContent": "",
                        "render" : function(data, type, full, meta) {
                            return "<a  data-code='"+full.packCode+"' data-start='"+full.startDateStr+"' data-end='"+full.endDateStr+"' class=\"detail\" style=\"cursor:pointer;color:blue;\" >"+data+"</a>";
                        }
                    }, {
                        "data": "workCount",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "workTimeSpan",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "maxCurrentValue",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "overproofCurrentCount",
                        "bSortable": false,
                        "defaultContent": "",
                        "sWidth": "50px"
                    }, {
                        "data": "chargeCount",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "chargeTimeSpan",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "maxVolDiffDateStr",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "maxVolDiffValue",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "maxTempDiffDateStr",
                        "bSortable": false,
                        "defaultContent": ""
                    }, {
                        "data": "maxTempDiffValue",
                        "bSortable": false,
                        "defaultContent": ""
                    }
                ],
                "fnDrawCallback": function () {
                    util.clearSelected(tableId);
                }
            });
        },
        _query:function () {
            //查询
            $("#btn_query").click(function(e) {
                var startDateStr = $("#gatherTime_start").val().trim();
                var startDate = new Date(startDateStr);
                var startDay = startDate.getDay();
                var endDateStr = $("#gatherTime_end").val().trim();
                var endDate = new Date(endDateStr);
                var endDay = endDate.getDay();
                if (startDay !==1||endDay!==0) {
                    rmsBox.notify('warning', {msg: '请选择正确的开始时间和结束时间！',delay:5000});
                    return
                }
                var table = $('#weeklyReport_table').DataTable();
                table.draw(true);
            });
            //重置查询条件
            $("#btn_reset").click(function(e) {
                $("#customer_search_form")[0].reset();
                weeklyReportTable._initDate();
            });
        },
        _export: function () {

            $("#weeklyReport_table_exp").on("click", function (e) {
                e.preventDefault();
                var aoData = [];
                aoData.push({
                    "name": "startDate",
                    "value": $("#gatherTime_start").val().trim() + " 00:00:00"
                });
                aoData.push({
                    "name": "endDate",
                    "value": $("#gatherTime_end").val().trim() + " 23:59:59"
                });
                $.fileDownload('main/report/weekly/export.mvc', {
                    httpMethod: 'POST',
                    data: {"aoData": JSON.stringify(aoData)},
                    prepareCallback: function (url) {

                    },
                    successCallback: function (url) {
                        rmsBox.notify('success', {msg: '操作成功'});
                    },
                    failCallback: function (html, url) {
                        rmsBox.notify('error', {msg: '操作失败！', delay: 3000});
                    }
                });
            });

        },
        _showDetail: function () {
            $("#weeklyReport_table").on('click', 'a.detail', function(e) {
                var packCode = $(this).attr("data-code");
                var startDate = $(this).attr("data-start")+" 00:00:00";
                var endDate = $(this).attr("data-end")+ "23:59:59";
                $.ajax({
                    url: 'main/report/weekly/findAlertDetail.mvc',
                    type: "post",
                    dataType: 'json',
                    data: {
                        packCode:packCode,
                        startDate:startDate,
                        endDate:endDate
                    },
                    success: function (response) {
                        if (response.result === true) {
                            var _detail = response.data;
                            var alertDetail ="";
                            for (var i =0;i<_detail.length;i++) {
                                alertDetail = alertDetail + _detail[i].name + ":" + _detail[i].value;
                                if (i < _detail.length - 1) {
                                    alertDetail = alertDetail + "\r\n";
                                }
                            }
                            $("#alertDetail").val(alertDetail);
                            $('#alertDetailModal').modal({show:true,keyboard:false});
                        } else {

                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
            });
        },
        _initDate: function () {
            $("#gatherTime_start").val(weeklyReportTable._getLastWeekStart);
            $("#gatherTime_end").val(weeklyReportTable._getLastWeekEnd);
        },
        _getLastWeekStart: function () {
            var current = new Date();
            var day = current.getDay() - 1;
            var date = weeklyReportTable._addDate(current, (day + 7) * -1);
            return weeklyReportTable._formatDate(date);
        },
        _getLastWeekEnd: function () {
            var current = new Date();
            var day = current.getDay();
            var date = weeklyReportTable._addDate(current, day * -1);
            return weeklyReportTable._formatDate(date);
        },
        _formatDate: function (date) {
            var year = date.getFullYear();
            var month = (date.getMonth() + 1);
            var day = date.getDate();
            return year + "-" + month + "-" + day;
        },
        _addDate: function (date, n) {
            date.setDate(date.getDate() + n);
            return date;
        }


    }
}();